package com.alienlab.finder.web.rest;
import com.alienlab.finder.domain.TemporaryPosition;
import com.alienlab.finder.service.TemporaryPositionService;
import com.alienlab.finder.web.rest.errors.BadRequestAlertException;
import com.alienlab.finder.web.rest.util.ErrorResult;
import com.alienlab.finder.web.rest.util.HeaderUtil;
import com.alienlab.finder.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing TemporaryPosition.
 */
@RestController
@RequestMapping("/api")
public class TemporaryPositionResource {

    private final Logger log = LoggerFactory.getLogger(TemporaryPositionResource.class);

    private static final String ENTITY_NAME = "temporaryPosition";

    private final TemporaryPositionService temporaryPositionService;

    public TemporaryPositionResource(TemporaryPositionService temporaryPositionService) {
        this.temporaryPositionService = temporaryPositionService;
    }



    @ApiOperation(value = "开启/停止查看实时位置")
    @PostMapping("/temporary-positions/looking")
    public ResponseEntity lookTemporaryPosition(@RequestParam String admin_open_id,@RequestParam String guest_open_id,@RequestParam String flag,@RequestParam String type) throws URISyntaxException {
        log.debug("REST request to 开启/停止查看实时位置");
        if ("".equals(admin_open_id) || admin_open_id == null) {
            throw new BadRequestAlertException("开启/停止查看实时位置", ENTITY_NAME, "admin_open_id donot exist");
        }
        if ("".equals(guest_open_id) || guest_open_id == null) {
            throw new BadRequestAlertException("开启/停止查看实时位置", ENTITY_NAME, "guest_open_id donot exist");
        }
        if ("".equals(flag) || flag == null) {
            throw new BadRequestAlertException("开启/停止查看实时位置", ENTITY_NAME, "flag donot exist");
        }
        if ("".equals(type) || type == null) {
            throw new BadRequestAlertException("开启/停止查看实时位置", ENTITY_NAME, "type donot exist");
        }
        try {
            temporaryPositionService.lookingPosition(admin_open_id,guest_open_id, flag,type);
            return ResponseEntity.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return  ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResult());
        }

    }
    @ApiOperation(value = "发送实时位置")
    @PostMapping("/temporary-positions/send")
    public ResponseEntity sendTemporaryPosition(@RequestParam String admin_open_id,@RequestParam String position_x,@RequestParam String position_y) throws URISyntaxException {
        log.debug("REST request to 发送实时位置");
        if ("".equals(admin_open_id) || admin_open_id == null) {
            throw new BadRequestAlertException("发送实时位置", ENTITY_NAME, "admin_open_id donot exist");
        }
        if ("".equals(position_x) || position_x == null) {
            throw new BadRequestAlertException("发送实时位置", ENTITY_NAME, "position_x donot exist");
        }
        if ("".equals(position_y) || position_y == null) {
            throw new BadRequestAlertException("发送实时位置", ENTITY_NAME, "y donot exist");
        }
        try {
            temporaryPositionService.sendPosition(admin_open_id, position_x, position_y);
            return ResponseEntity.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return  ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResult());
        }

    }
    /**
     * POST  /temporary-positions : Create a new temporaryPosition.
     *
     * @param temporaryPosition the temporaryPosition to create
     * @return the ResponseEntity with status 201 (Created) and with body the new temporaryPosition, or with status 400 (Bad Request) if the temporaryPosition has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/temporary-positions")
    public ResponseEntity<TemporaryPosition> createTemporaryPosition(@Valid @RequestBody TemporaryPosition temporaryPosition) throws URISyntaxException {
        log.debug("REST request to save TemporaryPosition : {}", temporaryPosition);
        if (temporaryPosition.getId() != null) {
            throw new BadRequestAlertException("A new temporaryPosition cannot already have an ID", ENTITY_NAME, "idexists");
        }
        TemporaryPosition result = temporaryPositionService.save(temporaryPosition);
        return ResponseEntity.created(new URI("/api/temporary-positions/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /temporary-positions : Updates an existing temporaryPosition.
     *
     * @param temporaryPosition the temporaryPosition to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated temporaryPosition,
     * or with status 400 (Bad Request) if the temporaryPosition is not valid,
     * or with status 500 (Internal Server Error) if the temporaryPosition couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/temporary-positions")
    public ResponseEntity<TemporaryPosition> updateTemporaryPosition(@Valid @RequestBody TemporaryPosition temporaryPosition) throws URISyntaxException {
        log.debug("REST request to update TemporaryPosition : {}", temporaryPosition);
        if (temporaryPosition.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        TemporaryPosition result = temporaryPositionService.save(temporaryPosition);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, temporaryPosition.getId().toString()))
            .body(result);
    }

    /**
     * GET  /temporary-positions : get all the temporaryPositions.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of temporaryPositions in body
     */
    @GetMapping("/temporary-positions")
    public ResponseEntity<List<TemporaryPosition>> getAllTemporaryPositions(Pageable pageable) {
        log.debug("REST request to get a page of TemporaryPositions");
        Page<TemporaryPosition> page = temporaryPositionService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/temporary-positions");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /temporary-positions/:id : get the "id" temporaryPosition.
     *
     * @param id the id of the temporaryPosition to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the temporaryPosition, or with status 404 (Not Found)
     */
    @GetMapping("/temporary-positions/{id}")
    public ResponseEntity<TemporaryPosition> getTemporaryPosition(@PathVariable Long id) {
        log.debug("REST request to get TemporaryPosition : {}", id);
        Optional<TemporaryPosition> temporaryPosition = temporaryPositionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(temporaryPosition);
    }

    /**
     * DELETE  /temporary-positions/:id : delete the "id" temporaryPosition.
     *
     * @param id the id of the temporaryPosition to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/temporary-positions/{id}")
    public ResponseEntity<Void> deleteTemporaryPosition(@PathVariable Long id) {
        log.debug("REST request to delete TemporaryPosition : {}", id);
        temporaryPositionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
